/**
 * <p>Copyright: Copyright (c) 2012</p>
 * <p>Company: 联动优势科技有限公司</p>
 * <p>May 3, 20132:19:04 PM</p>
 * @author Ding Zhe
 * @version 1.0
 */
package com.umpay.privi.ejb.sys.operation;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.umpay.common.persist.ibatis.DataAccessException;
import com.umpay.privi.common.BeanManager;
import com.umpay.privi.common.ReqMessage;
import com.umpay.privi.common.ResMessage;
import com.umpay.privi.common.RetCode;
import com.umpay.privi.common.XmlData;
import com.umpay.privi.dao.on.sys.RoleDao;
import com.umpay.privi.dao.on.sys.UserDao;
import com.umpay.privi.exception.BusinessException;
import com.umpay.privi.helper.ReqParaCheckHelper;
import com.umpay.privi.model.sys.Role;
import com.umpay.privi.model.sys.User;
import com.umpay.privi.service.sys.operation.OperationRoleServiceRemote;

/**
 * 
 * desc:角色EJB
 * <p>创建人：liuguangjun 创建日期：May 22, 2013 </p>
 * @version V1.0
 */
@TransactionManagement(TransactionManagementType.BEAN)
public class OperationRoleServiceBean implements OperationRoleServiceRemote{
	private static Logger logger = LoggerFactory.getLogger(OperationRoleServiceBean.class);
	
	public ResMessage queryRolesWithPage(ReqMessage reqMessage) {
		logger.info(String.format("[根据条件分页查询所有角色][OperationRoleServiceBean]queryRolesWithPage请求参数：%s", reqMessage.toString()));
		ResMessage resMessage = new ResMessage();//创建报文返回对象
		//上送数据校验
		ResMessage checkPara = ReqParaCheckHelper.checkRequire(reqMessage, "OperationRoleServiceBean.queryRolesWithPage.require");
		if (!RetCode.SUCCESS.equals(checkPara.getRetCode())) {
			return checkPara;
		}
		try {
			//查询操作员信息
			Map<String, Object> qryMap = new HashMap<String, Object>();
			String sysId = reqMessage.getString(XmlData.SYSID);
			String operUserId = reqMessage.getString(XmlData.OPERUSERID);
			qryMap.put(XmlData.USERID, operUserId);
			qryMap.put(XmlData.SYSID, sysId);
			UserDao userDao = BeanManager.getBean("userDao", UserDao.class);
			User operUser = userDao.getUser(qryMap);
			if(operUser == null)throw new BusinessException(RetCode.NO_DATA,"操作员不存在！");
			Map<String, Object> reqMap=reqMessage.getReqMap();
			Integer totalNum=0;
			List<Role> roleList=null;
			RoleDao roleDao = BeanManager.getBean("roleDao", RoleDao.class);
			Integer startIndex = Integer.parseInt(reqMessage.getString(XmlData.START_INDEX));//开始记录索引
			Integer pageSize = Integer.parseInt(reqMessage.getString(XmlData.PAGE_SIZE));//每页显示条数
			//根据操作员身份查询不同的权限
			if(operUser.getRank()==0||operUser.getRank()==1){//超级管理员、系统管理员不用关联表查询,创建人是操作员的角色
				reqMap.put(XmlData.CREATOR,operUser.getMediaId());
				reqMap.put(XmlData.OPERRANK, operUser.getRank()); 
				roleList=roleDao.findPageList(reqMap, startIndex, pageSize);
				totalNum = roleDao.findPageList_count(reqMap);
			}else if(operUser.getRank()==2){//业务管理员查询到其所拥有的所有角色
				 roleList=roleDao.findRolePageListByKeys(reqMap, startIndex, pageSize);
				 totalNum = roleDao.findRolePageListByKeys_count(reqMap);
			}else{//操作员无权限
				throw new BusinessException(RetCode.NO_PRIVILEGE,"无操作权限");
			}
			resMessage.put(XmlData.RESULT_LIST, roleList);
			resMessage.put(XmlData.TOTAL_NUM, totalNum);
			resMessage.setRetCode(RetCode.SUCCESS);
			resMessage.setRetMsg("成功");
		}catch (BusinessException e) {
			logger.error("请求数据不正确",e);
			resMessage.setRetCode(e.getCode());
			resMessage.setRetMsg(e.getMessage());
		} catch (DataAccessException e) {
			logger.error(e.getMessage(),e);
			resMessage.setRetCode(RetCode.DB_ERROR);
			resMessage.setRetMsg("数据库出错");
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			resMessage.setRetCode(RetCode.UNKNOWN);
			resMessage.setRetMsg("未知错误");
		}
		return resMessage;
	}


}
